/**
下载图片demo

***/

package main

import (
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"net/url"
	"os"
	"strings"
	//	"path"
)

const urls string = "http://www.phpstar.cn/article/index/arcid/1652.html"

func main() {
	resp, _ := http.Get(urls)
	//解析文件的名称
	urlss, err := url.Parse(urls)
	if err != nil {
		fmt.Println("url 解析错误")
		return
	}
	urlString := urlss.String()
	//解析地址
	queryMap := strings.Split(urlString, "/")
	name := queryMap[len(queryMap)-1]
	fmt.Println(urlString)
	//fmt.Println(urls)

	defer resp.Body.Close()

	if resp.StatusCode == 200 {
		file, _ := os.Create(name)
		result, _ := ioutil.ReadAll(resp.Body)
		io.Copy(file, bytes.NewReader(result))
		defer file.Close()
	}

	fmt.Println("图片下载 保存完成")
}
